#    Supsta
#    Copyright (c) 2014 Sebastian Dufner
#
#    This file is part of Supsta.
#
#    Supsta is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Supsta is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'supsta/gimp/drawable'
require 'supsta/gimp/integer'
require 'supsta/gimp/layer'

module Supsta
  module Gimp

    ##
    # A GIMP item.

    class Item
      def initialize(scope, layer, image)
        @app, @self, @image = scope, layer, image
      end
      attr_reader :image

      ##
      # Returns the current Scope.

      def scope
        @app
      end

      ##
      # Returns the variable name this item is assigned to.

      def to_s
        @self.to_s
      end

      ##
      # Returns the item's position.

      def position
        var = @app.new_var
        scope.def var, "(car(gimp-image-get-item-position #{@image} #{@self}))"

        Integer.new(var)
      end

      ##
      # Lowers the item by +n+ levels.
      #
      # *Note:* Performs the operation +n+ times without sanity checks.
      #
      # *Note:* Integer values are currently unsupported.

      def lower(n = 1)
        @app.script.concat("(gimp-image-lower-item #{@image} #{@self})" * n)
            .concat "\n"

        self
      end

      ##
      # Lowers the item to the bottom of its stack.

      def lower_to_bottom
        @app.script.concat "(gimp-image-lower-item-to-bottom #{@image} #{@self})\n"

        self
      end

      ##
      # Raises the item by +n+ levels.
      #
      # *Note:* Performs the operation +n+ times without sanity checks.
      #
      # *Note:* Integer values are currently unsupported.

      def raise(n = 1)
        @app.script.concat("(gimp-image-raise-item #{@image} #{@self})" * n)
            .concat "\n"

        self
      end

      ##
      # Raises the item to the top of its stack.

      def raise_to_top
        @app.script.concat "(gimp-image-raise-item-to-top #{@image} #{@self})\n"

        self
      end

      ##
      # Deletes the item.

      def delete!
        @app.script.concat "(gimp-item-delete #{@self})\n"
        nil
      end

      ##
      # Returns a Layer representing this item.
      #
      # *Note:* Bypasses type-safety. Only use this if you are certain that
      # this item is in fact a Layer.

      def to_layer
        Layer.new(@app, @self, @image)
      end

      ##
      # Returns a Drawable representing this item.
      #
      # *Note:* Bypasses type-safety. Only use this if you are certain that
      # this item is in fact a Drawable.

      def to_drawable
        Drawable.new(@app, @self, @image)
      end

    end
  end
end
